package com.gitee.ly.ymorm.datasource.pool;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * YmOrmDataSourceFactory
 *
 * @author ly
 **/
public class DataSourceFactory implements com.gitee.ly.ymorm.datasource.DataSourceFactory {
    private DataSource dataSource;

    @Override
    public void setProperties(Properties properties) {
        //使用HikariCp作为底层数据库连接池
        HikariDataSource hikariDataSource = null;
        if (properties != null) {
            HikariConfig hikariConfig = new HikariConfig();
            hikariConfig.setDriverClassName(properties.getOrDefault("driver", "").toString());
            hikariConfig.setJdbcUrl(properties.getOrDefault("url", "").toString());
            hikariConfig.setUsername(properties.getOrDefault("username", "").toString());
            hikariConfig.setPassword(properties.getOrDefault("password", "").toString());
            hikariConfig.setMinimumIdle((Integer) properties.getOrDefault("minIdle", "-1"));
            hikariConfig.setMaximumPoolSize((Integer) properties.getOrDefault("maxPoolSize", "-1"));
            hikariDataSource = new HikariDataSource(hikariConfig);
        }
        dataSource = hikariDataSource;
    }

    @Override
    public DataSource getDataSource() {
        return dataSource;
    }
}
